import dayjs from 'dayjs'
import { IForm, ReserveParams, Status } from './types'

export const dateFormatter = 'YYYY-MM-DD'

export function toParams({
  // grade,
  reserveDate,
  reserveStartTime,
  reserveEndTime,
  maxNum,
  description,
  deadline,
  id,
  appointmentPublishCode,
}: IForm): ReserveParams {
  return {
    id,
    appointmentPublishCode,
    // gradeIds: grade?.value,
    deadline: reserveDate + ' 23:59:59',
    startTime: reserveDate + ' ' + reserveStartTime + ':00',
    endTime: reserveDate + ' ' + reserveEndTime + ':00',
    maxNum,
    description,
    status: Status.draft,
  }
}

function getHours(date: string | Date) {
  return (dayjs(date).get('h') + '').padStart(2, '0')
}

function getMinutes(date: string | Date) {
  return (dayjs(date).get('m') + '').padStart(2, '0')
}

export function getTime(date: string | Date) {
  // return dayjs(date).format('hh:mm')
  return `${getHours(date)}:${getMinutes(date)}`
}

export function toForm({
  // gradeIds = '',
  deadline,
  startTime,
  endTime,
  maxNum,
  description,
  status,
  id,
  appointmentPublishCode,
  remainder,
}: ReserveParams): IForm {
  return {
    id,
    appointmentPublishCode,
    // grade: { name: gradeIds, value: gradeIds },
    reserveDate: dayjs(startTime).format(dateFormatter),
    reserveStartTime: getHours(startTime) + ':' + getMinutes(startTime),
    reserveEndTime: getHours(endTime) + ':' + getMinutes(endTime),
    maxNum,
    description,
    status,
    deadline: dayjs(deadline).format(dateFormatter),
    remainder,
  }
}

export const getLaterDate = (num: number) => {
  let date = dayjs().add(num, 'day').format('YYYY-MM-DD')

  return date
}
